package com.freshfresh.activity.selfcenter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.wheel.JudgeDate;
import com.freshfresh.view.wheel.ScreenInfo;
import com.freshfresh.view.wheel.WheelMain;

public class ModifyGenderOldActivity extends BaseActivity implements
		OnClickListener {
	private Dialog alertDialog;
	private Button bt_commit_gender_old;
	private String defaultDate;
	private String genderValue = "";
	private ImageView iv_select_man;
	private ImageView iv_select_woman;
	private TextView tv_birthday;
	Dialog progressDialog;
	TextView tv_title;
	
	WheelMain wheelMain;
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	String str_date;

	private boolean canModify() {
		if (TextUtils.isEmpty(this.genderValue)) {
			Toast.makeText(ModifyGenderOldActivity.this, "请选择性别", 0).show();
			return false;
		}
		if (TextUtils.isEmpty(this.tv_birthday.getText())) {
			Toast.makeText(ModifyGenderOldActivity.this, "请选择生日", 0).show();
			return false;
		}
		return true;
	}

	private void initClick() {
		this.iv_select_man.setOnClickListener(this);
		this.iv_select_woman.setOnClickListener(this);
		this.tv_birthday.setOnClickListener(this);
		this.bt_commit_gender_old.setOnClickListener(this);
		findViewById(R.id.rel_back).setOnClickListener(this);
	}

	private void initView() {
		progressDialog = Utils.createLoadingDialog(ModifyGenderOldActivity.this, "正在加载……");
		this.iv_select_man = ((ImageView) findViewById(R.id.iv_select_man));
		this.iv_select_woman = ((ImageView) findViewById(R.id.iv_select_woman));
		this.tv_birthday = ((TextView) findViewById(R.id.tv_birthday));
		this.bt_commit_gender_old = ((Button) findViewById(R.id.bt_commit_gender_old));
		tv_title=(TextView)findViewById(R.id.tv_title);
		tv_title.setText("我的资料");
	}

	private void modifyGenderOldRequest() {
		
		
		
		progressDialog.show();
		SharedPreferences userShared = FreshConstants.getUserShared(ModifyGenderOldActivity.this);
		String customerid=userShared.getString("userid", "");
		String token=userShared.getString("token", "");
		String mobile=userShared.getString("mobile", "");
		Map<String, String> params = new HashMap<String, String>();
		params.put("customerid", customerid);
		params.put("token", token);
		params.put("mobile", mobile);
		
		params.put("birthday", this.tv_birthday.getText().toString());
		params.put("sex", this.genderValue);
		
		Log.e("birthday是多少………………", this.tv_birthday.getText().toString());
		Log.e("sex是多少………………", genderValue);
		executeRequest(new StringRequest(UrlConstants.getUserCommitBrith, params,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("arg0个人信息是多少………………", arg0);
						if (!Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
							showToast(ModifyGenderOldActivity.this, "修改失败");
						} else {
							showToast(ModifyGenderOldActivity.this, "修改成功");
							
							SharedPreferences userShared = FreshConstants.getUserShared(ModifyGenderOldActivity.this);
							Editor editor=userShared.edit();
							editor.putString("sex", genderValue);
							editor.putString("birthday", tv_birthday.getText().toString());
							editor.commit();
							
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						progressDialog.dismiss();
					}
				}));
		
		
		
		
		
		
	}

	private void setViewData() {
		SharedPreferences sharep=FreshConstants.getUserShared(ModifyGenderOldActivity.this);
		String str_birth=sharep.getString("birthday", "");
		String str_sex=sharep.getString("sex", "");
		Log.e("str_birth^^^^^^^^^^", str_birth);
		Log.e("str_sex^^^^^^^^^^", str_sex);
//		UserInfo localUserInfo = getUserInfo();
//		String str1 = localUserInfo.getBirthday();
		
		if ((!TextUtils.isEmpty(str_sex)) && (!"null".equals(str_sex))){
			if ("男".equals(str_sex)){
				this.genderValue = "男";
				this.iv_select_man.setImageDrawable(getResources().getDrawable(
						R.drawable.gender_old_select));
				this.iv_select_woman.setImageDrawable(getResources().getDrawable(
						R.drawable.gender_old_no_select));
			}else{
				this.genderValue = "女";
				this.iv_select_man.setImageDrawable(getResources().getDrawable(
						R.drawable.gender_old_no_select));
				this.iv_select_woman.setImageDrawable(getResources().getDrawable(
						R.drawable.gender_old_select));
			}
		}
		
		if ((!TextUtils.isEmpty(str_birth)) && (!"null".equals(str_birth))){
			tv_birthday.setText(str_birth);
		}
		
		
	}

	private void showDialog() {
		this.alertDialog = new Dialog(this, R.style.MyDialogStyle);
		this.alertDialog.show();
		Display localDisplay = getWindowManager().getDefaultDisplay();
		int i = localDisplay.getWidth();
		localDisplay.getHeight();
		this.alertDialog.getWindow().setLayout(i * 3 / 4, -2);
	}

	private void showbritherDialog() {
		showDialog();
		this.alertDialog.setCanceledOnTouchOutside(true);
		this.alertDialog.getWindow().setContentView(2130903179);
		final DatePicker localDatePicker = (DatePicker) this.alertDialog
				.getWindow().findViewById(2131165944);
		Calendar localCalendar1 = Calendar.getInstance();
		localDatePicker.init(localCalendar1.get(1), localCalendar1.get(2),
				localCalendar1.get(5), null);
		localDatePicker.setMaxDate(System.currentTimeMillis());
		SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd");
		try {
			Calendar localCalendar2 = Calendar.getInstance();
			localCalendar2.setTime(localSimpleDateFormat
					.parse(this.defaultDate));
			localDatePicker.updateDate(localCalendar2.get(1),
					localCalendar2.get(2), localCalendar2.get(5));
			TextView localTextView = (TextView) this.alertDialog.getWindow()
					.findViewById(2131165942);
			((TextView) this.alertDialog.getWindow().findViewById(2131165943))
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View paramAnonymousView) {
							int i = 1 + localDatePicker.getMonth();
							ModifyGenderOldActivity.this.defaultDate = (localDatePicker
									.getYear() + "-" + i + "-" + localDatePicker
									.getDayOfMonth());
							ModifyGenderOldActivity.this.tv_birthday
									.setText(localDatePicker.getYear() + "-"
											+ i + "-"
											+ localDatePicker.getDayOfMonth());
							ModifyGenderOldActivity.this.alertDialog.dismiss();
						}
					});
			localTextView.setOnClickListener(new View.OnClickListener() {
				public void onClick(View paramAnonymousView) {
					ModifyGenderOldActivity.this.alertDialog.dismiss();
				}
			});
			return;
		} catch (ParseException localParseException) {
			while (true) {
				localParseException.printStackTrace();
			}
		}
	}


	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.activity_modify_gender_old);
		initView();
		
		Calendar calendar = Calendar.getInstance();
		str_date=calendar.get(Calendar.YEAR) + "-" +
				    (calendar.get(Calendar.MONTH) + 1 )+ "-" +
				    calendar.get(Calendar.DAY_OF_MONTH) + "";
//		tv_birthday.setText(calendar.get(Calendar.YEAR) + "-" +
//	  				    (calendar.get(Calendar.MONTH) + 1 )+ "-" +
//	  				    calendar.get(Calendar.DAY_OF_MONTH) + "");
		
		setViewData();
		initClick();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		default:
		case R.id.rel_back:
			finish();
			break;
		case R.id.iv_select_man:
			this.genderValue = "男";
			this.iv_select_man.setImageDrawable(getResources().getDrawable(
					R.drawable.gender_old_select));
			this.iv_select_woman.setImageDrawable(getResources().getDrawable(
					R.drawable.gender_old_no_select));
			break;
		case R.id.iv_select_woman:
			this.genderValue = "女";
			this.iv_select_man.setImageDrawable(getResources().getDrawable(
					R.drawable.gender_old_no_select));
			this.iv_select_woman.setImageDrawable(getResources().getDrawable(
					R.drawable.gender_old_select));
			break;
		case R.id.tv_birthday:
			
			
			LayoutInflater inflater=LayoutInflater.from(ModifyGenderOldActivity.this);
			final View timepickerview=inflater.inflate(R.layout.timepicker, null);
			ScreenInfo screenInfo = new ScreenInfo(ModifyGenderOldActivity.this);
			wheelMain = new WheelMain(timepickerview);
			wheelMain.screenheight = screenInfo.getHeight();
			String time = str_date;
			Calendar calendar = Calendar.getInstance();
			if(JudgeDate.isDate(time, "yyyy-MM-dd")){
				try {
					calendar.setTime(dateFormat.parse(time));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			wheelMain.initDateTimePicker(year,month,day);
			new AlertDialog.Builder(ModifyGenderOldActivity.this)
			.setTitle("选择时间")
			.setView(timepickerview)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					tv_birthday.setText(wheelMain.getTime());
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			})
			.show();
			
			
			
			break;
		case R.id.bt_commit_gender_old:
			while (!canModify());
			modifyGenderOldRequest();
			break;
		}
	}
}